package cn.imvc.app.controllers.member;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import cn.imvc.app.entities.DealerLevel;
import cn.imvc.app.entities.Group;
import cn.imvc.app.entities.PriceClass;
import cn.imvc.app.models.DealerLevelModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

/**
 * 
 * @author wujx
 * @time 2016年7月16日
 *
 */
public class DealerLevelControllerImpl implements DealerLevelController {
	static Logger logger = Logger.getLogger(DealerLevelControllerImpl.class);
	@Autowired
	private DealerLevelModel dealerLevelModel;
	public Page<DealerLevel> page;
	public ViewRequest viewRequest;
	
	private DealerLevel dealerLever;
	private int id;
	private String dealerLevelTree;
	private List<PriceClass> priceClassList;
	private int priceClassId;
	private Double theJoinMoney;
	
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		//按照等级高低，从低到高排序
		if(viewRequest == null){
			viewRequest = new ViewRequest();
		}
			viewRequest.getSort().put("level", "AEC");
		page = dealerLevelModel.find(viewRequest);
		return "dealer-level/index";
	}

	public String show() {
		dealerLever = dealerLevelModel.getOne(id);
		return "dealer-level/show";
	}

	public String editNew() {
		priceClassList= dealerLevelModel.getPriceClass();
		return "dealer-level/edit";
	}
	public String create() {
		dealerLever.setJoinMoney((int)(theJoinMoney*100));
		if(dealerLevelModel.save(priceClassId,dealerLever)){
			return Render.json(new DialogBean("200", "创建成功","","closeCurrent"));
		}else{
			return Render.json(new DialogBean("300", "等级名字或等级级别相同！","","closeCurrent"));
		}
	}

	public String edit() {
		dealerLever = dealerLevelModel.getOne(id);
		priceClassList= dealerLevelModel.getPriceClass();
		return "dealer-level/edit";
	}

	public String update() {
		loggingModel.info(logger,"修改会员等级信息，等级："+id);
		dealerLever.setJoinMoney((int)(theJoinMoney*100));
		if(dealerLevelModel.update(priceClassId,dealerLever,id)){
			return Render.json(new DialogBean("200", "更新成功","","closeCurrent"));
		}else{
			return Render.json(new DialogBean("300", "等级名字或等级级别相同！","","closeCurrent"));
		}
	}

	public String destroy() {
		loggingModel.info(logger,"删除会员等级信息，等级："+id);
		String result = dealerLevelModel.delete(id);
		if(result.equals("")){
			return Render.json(new DialogBean("200", "删除成功",""));
		}else{
			return Render.json(new DialogBean("300", result,""));
		}
	}

	public String destroyBatch() {
		return null;
	}
	
	/**
	 * 供选择的dialog
	 * @return
	 */
	public String selectDialog() {
		dealerLevelTree = dealerLevelModel.treeLookup();
		return "dealer-level/selectDialog";
	}
	
	/**
	 * 测试
	 */
	public String ceshi(){
		return "dealer-level/ceshi";
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public Page<DealerLevel> getPage() {
		return page;
	}

	public void setPage(Page<DealerLevel> page) {
		this.page = page;
	}

	public DealerLevel getDealerLever() {
		return dealerLever;
	}

	public void setDealerLever(DealerLevel dealerLever) {
		this.dealerLever = dealerLever;
	}

	public String getDealerLevelTree() {
		return dealerLevelTree;
	}

	public void setDealerLevelTree(String dealerLevelTree) {
		this.dealerLevelTree = dealerLevelTree;
	}

	public List<PriceClass> getPriceClassList() {
		return priceClassList;
	}

	public void setPriceClassList(List<PriceClass> priceClassList) {
		this.priceClassList = priceClassList;
	}

	public int getPriceClassId() {
		return priceClassId;
	}

	public void setPriceClassId(int priceClassId) {
		this.priceClassId = priceClassId;
	}

	public Double getTheJoinMoney() {
		return theJoinMoney;
	}

	public void setTheJoinMoney(Double theJoinMoney) {
		this.theJoinMoney = theJoinMoney;
	}

}
